//
//  NSString+Extension.m
//  解税宝
//
//  Created by 边文辉 on 2016/10/27.
//  Copyright © 2016年 jieshuibao. All rights reserved.
//

#import "NSString+Extension.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (Extension)

+ (BOOL)isBlank:(NSString*)str
{
    BOOL ret = NO;
    if ((str == nil)
        || (str == NULL)
        || [str isKindOfClass:[NSNull class]]
        || ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0))
        ret = YES;
    return ret;
}

- (BOOL)isBlank
{
    
    if (self == nil
        || self == NULL
        || [self isEqual:[NSNull null]]
        || !self.length
        || ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)) {
        
        return YES;
    }
    
    return NO;
}

+ (BOOL)isPhoneNumber:(NSString *)string
{
    if (![string hasPrefix:@"1"]) {
        return NO;
    }
    NSString * MOBILE = @"^[1-9]\\d{10}$";
    NSPredicate *regextestMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return  [regextestMobile evaluateWithObject:string];
}

//+ (BOOL)isTelephones:(NSString *)string
//{
//    NSString * MOBILE = @"^[1-9]\\d{7}$";
//    NSPredicate *regextestMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
//    return  [regextestMobile evaluateWithObject:string];
//}

+ (BOOL)checkTelNumber:(NSString *) telNumber
{
    NSString *pattern = @"^1+[34578]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}



+ (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"^[a-zA-Z0-9]{6,15}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
    
}


+ (NSString *)encodeString:(NSString *)string
{
    return [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

+ (NSString *)decodeString:(NSString *)string
{
    return [string stringByRemovingPercentEncoding];
}

- (NSString *)encodeURLString
{
    return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

- (NSString *)decodeURLString
{
    return [self stringByRemovingPercentEncoding];
}

//判断是否为整形：
- (BOOL)isPureInt
{
    int val;
    NSScanner* scan = [NSScanner scannerWithString:self];
    return[scan scanInt:&val] && [scan isAtEnd];
}

//判断是否为浮点形：
- (BOOL)isPureFloat
{
    float val;
    NSScanner* scan = [NSScanner scannerWithString:self];
    return[scan scanFloat:&val] && [scan isAtEnd];
}


//字典转为Json字符串
+(NSString *)dictionaryToJson:(NSDictionary *)dic
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}



- (NSString *)hyb_toMD5 {
    if (self == nil || [self length] == 0) {
        return nil;
    }
    
    unsigned char digest[CC_MD5_DIGEST_LENGTH], i;
    CC_MD5([self UTF8String], (int)[self lengthOfBytesUsingEncoding:NSUTF8StringEncoding], digest);
    NSMutableString *ms = [NSMutableString string];
    
    for (i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [ms appendFormat:@"%02x", (int)(digest[i])];
    }
    
    return [ms copy];
}


//获取字符串的宽度
+(float) widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height
{
     CGSize sizeToFit = [value boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize], NSFontAttributeName, nil] context:nil].size;

    return sizeToFit.width;
}

+ (NSArray *)stringFromStr:(NSString *)string {
    
    if ([string isBlank]) {
        return nil;
    }
    
    NSString *str1 = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if ([str1 isBlank]) {
        return nil;
    }
    
    if ([str1 containsString:@","]) {
        NSArray* array = [str1 componentsSeparatedByString:@","];
        
        if (array.count) {
            return array;
        } else {
            return nil;
        }
    }
    
    return nil;

}

@end
